package com.sanpu.kxe.module.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.login.view.SplashActivity;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.IntentTool;

import butterknife.ButterKnife;

/**
 * Description：交易推送消息弹框
 * Created by 薛勇军 on 2019/3/22.
 */

public class PushMsgTransactionDialog {

    private String content;
    private TextView vContent;
    private Context context;
    private int layout;
    private String amount;
    private String cashAmount;
    private String timestamp;
    private String currencyName;

    public PushMsgTransactionDialog targetView(@LayoutRes int layout) {
        this.layout = layout;
        return this;
    }

    public PushMsgTransactionDialog amount(String amount) {
        this.amount = amount;
        return this;
    }

    public PushMsgTransactionDialog timestamp(String timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public PushMsgTransactionDialog cashAmount(String cashAmount) {
        this.cashAmount = cashAmount;
        return this;
    }

    public PushMsgTransactionDialog currencyName(String currencyName) {
        this.currencyName = currencyName;
        return this;
    }

    public PushMsgTransactionDialog(Context context) {
        this.context = context;
    }

    public void show() {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final View view = View.inflate(context, layout, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeView(view);
            }
        });
        RoundTextView cancelTv = ButterKnife.findById(view, R.id.cancel);
        RoundTextView comfirmTv = ButterKnife.findById(view, R.id.ok);
        try {
            cancelTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    windowManager.removeView(view);
                }
            });

            comfirmTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    windowManager.removeView(view);
                    Intent intent = new Intent(context, SplashActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("isTransPush", true);
                    IntentTool.startActivity(context, intent);
                }
            });
            boolean isForeground = CacheManage.getInstance().getCache(CacheModel.ISFOREGROUND);
            if (isForeground) {
                cancelTv.setText("朕知道了");
                cancelTv.getDelegate().setBackgroundColor(ContextCompat.getColor(context, R.color.color_base));
                cancelTv.setTextColor(ContextCompat.getColor(context, R.color.white));
                comfirmTv.setVisibility(View.GONE);
            }
            ButterKnife.findById(view, R.id.background).setAnimation(AnimationUtils.loadAnimation(context, R.anim.alpha_in));
            String unit = TextUtils.isEmpty(currencyName)?"元":currencyName;
            ((TextView) ButterKnife.findById(view, R.id.amount)).setText(Html.fromHtml("收到金额：<font color='#FF3F40'>" + amount + "</font>"+unit));

            if (!TextUtils.isEmpty(cashAmount)) {
                ButterKnife.findById(view, R.id.amount_layout).setVisibility(View.VISIBLE);
                ((TextView) ButterKnife.findById(view, R.id.cash_amount)).setText(cashAmount + unit);
            }

            if (!TextUtils.isEmpty(amount) && !TextUtils.isEmpty(timestamp)) {
                ((TextView) ButterKnife.findById(view, R.id.time)).setText(timestamp);
            }

            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //26及以上必须使用TYPE_APPLICATION_OVERLAY   @deprecated TYPE_PHONE
                params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                params.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            params.format = PixelFormat.TRANSLUCENT;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            params.gravity = Gravity.TOP | Gravity.LEFT;
            windowManager.addView(view, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
